#-*- encoding: utf-8 -*-
'''
Created on 2011-4-17

@author: Bob
'''
from twisted.internet.protocol import Protocol, ReconnectingClientFactory
from sys import stdout
from twisted.protocols.basic import Int32StringReceiver
from jxgame.proto.land_pb2 import GR_LogonResponse
from google.protobuf import text_format

class LandProtocol(Int32StringReceiver):
    def stringReceived(self,string):
        y = GR_LogonResponse()
        y.ParseFromString(string)
        s = text_format.MessageToString(y, True, False)
        print "y:\r\n" + s

#    def dataReceived(self, data):
#        stdout.write(data)

class LandClientFactory(ReconnectingClientFactory):
    def startedConnecting(self, connector):
        print 'Started to connect.'

    def buildProtocol(self, addr):
        print 'Connected.'
        print 'Resetting reconnection delay'
        self.resetDelay()
        return LandProtocol()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection.  Reason:', reason
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason
        ReconnectingClientFactory.clientConnectionFailed(self, connector,
                                                         reason)